﻿using System;
using System.IO;
using System.Linq;
using System.Web;

namespace JULONG.WEB.Library
{
    /// <summary>
    /// 文件上传下载处理类
    /// </summary>
    public class Upload
    {
        /// <summary>
        /// 文件类型验证 “|”分割,不区分大小写 留空则可以全部上传
        /// </summary>
        public string fileExts = "";

        /// <summary>
        /// 文件是否改名存储
        /// </summary>
        public bool isRename = true;

        /// <summary>
        /// 文件最大判定 不判定为0
        /// </summary>
        public int fileMaxSize = 0;

        /// <summary>
        /// 上传目录 注意前不要加"/" 后加"/" 
        /// </summary>
        public string rootDirectory = "Content/uploads/product/";


        /// <summary>
        /// 存储文件
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public string save(HttpPostedFileBase file)
        {
            if (!HasFile(file))
                throw new Exception("文件大小错误！");

            if (fileMaxSize!=0 && file.ContentLength > fileMaxSize)
                throw new Exception("文件过大！");

            if (!string.IsNullOrEmpty(fileExts))
            {
                string Ext = Path.GetExtension(file.FileName).ToLower();
                if (fileExts.ToLower().Split('|').Contains(Ext))
                    new Exception("该类型文件禁止上传！");
            }

            //创建目录
            var path = createPath(rootDirectory);

            //文件名
            var fileName = createFilename(ref file);

            try
            {
                file.SaveAs(Path.Combine(path, fileName));
            }
            catch
            {
                new Exception("文件上传失败！");
            }

            return rootDirectory + fileName;
        }

 
        //检测文件
        public static bool HasFile(HttpPostedFileBase file)
        {
            if (file == null) return false;
            if (file.ContentLength == 0) return false;
            return true;
        }
        
        /// <summary>
        /// 创建目录并返回完全路径
        /// </summary>
        /// <param name="path"></param>
        private string createPath(string path)
        {
            path = AppDomain.CurrentDomain.BaseDirectory + path;
            if (Directory.Exists(path)) return path;
            
            try
            {
                Directory.CreateDirectory(path);
            }
            catch
            {
                throw new Exception("不能创建目录!");
            }
            return path;
        }

        /// <summary>
        /// 创建文件名
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        private string createFilename(ref HttpPostedFileBase file)
        {
            if(!isRename) 
                return Path.GetFileName(file.FileName).ToLower();

            string dateName = DateTime.Now.ToString("yyyyMMddHHmmss");
            string Guid = System.Guid.NewGuid().ToString().Split('-')[1];
            return dateName + "_" + Guid + Path.GetExtension(file.FileName).ToLower();
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="path">针对于站点的绝对路径 如： /Content/uploads/xxxx.jpg</param>
        /// <returns>是否成功</returns>
        public static bool del(string filePath)
        {
            try
            {
                FileInfo file = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + filePath);
                file.Delete();
            }
            catch
            {
                return false;
            }
            return true;
        }
        

        /// <summary>
        /// 得到文件大小
        /// </summary>
        /// <param name="path">文件路径 相对站点根</param>
        /// <returns>文件大小 单位 K</returns>
        public static long getFileSize(string filePath)
        {
            FileInfo fi;
            try
            {
                fi = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + filePath);
                return fi.Length / 1000;
            }
            catch
            {
                return 0;
            }
        }

    }

}